package cn.dglydrpy.study.j2ee.servlet.day08;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.pool.impl.GenericKeyedObjectPool.Config;
/*
 * servlet的执行过程及得到全局初始化数据 绑定上下文 绑定session数据
 */
public class SomeServlet2 extends HttpServlet {


	public SomeServlet2() {//servlet永远只能有一个实例化对象(执行一次)
		System.out.println("1.someServlet2的构造方法");
	}

	/*@Override  //service()和doxxx()选择其中一个来重写,若都重写仅重写的service有效
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("3.someServlet2的service()");
	}*/

	@Override
	public void init() throws ServletException {//重写无参的init()来实现对初始化的拓展功能 //初始化也只执行一次
		System.out.println("2.SomeServlet2的初始化处理逻辑");
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//处理get请求的方法,有get请求即执行此方法
		System.out.println("3.SomeServlet2的doGet()");

		ServletConfig config = getServletConfig();//继承自GenericServlet类的方法直接获取ServletConfig对象

		//读取初始化参数
		String company = config.getInitParameter("company");
		System.out.println("company: "+company);

		//获取上下文
		ServletContext context = getServletContext();
		System.out.println("context: "+ context);
		//绑定数据
		context.setAttribute("food", "佛跳墙");

		HttpSession session = request.getSession();
		session.setAttribute("user","老王");

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//处理post请求的方法,有post请求即执行该方法
		System.out.println("3.SomeServlet2的doPost()");
	}

	@Override
	public void destroy() {
		System.out.println("4.SomeServlet2的销毁处理逻辑");//在 应用 退出之前会进行销毁，但具体什么时候销毁我们无法控制(执行1次)
	}
}
